import { getFormData } from './api'

export default {
  data() {
    return {
      type: this.$route.params.type,
      bizId: this.$route.params.bizId
    }
  },
  computed: {
    flowConfig() {
      let temp = {}
      if (this.type === 'view' || this.type === 'execute') {
        Object.keys(this.formData).forEach((key) => {
          temp[key] = 'readonly'
        })
      }
      const taskKey = this.$route.params.taskKey
      if (
        this.type === 'execute' &&
        (taskKey === 'UserTask_0' || taskKey === 'fawkes_custom_flow_start')
      ) {
        Object.keys(this.formData).forEach((key) => {
          temp[key] = ''
        })
      }
      this.customConfig && this.customConfig(temp)
      return temp
    }
  },
  created() {
    this.$emit('setFormName', this.formName) //设置表单左上角弹窗名称
    this.$emit('setModelKey', this.modelKey) //设置modelKey
    this.$emit('setEntityName', this.entityName) //设置实体对象名称，需要询问后端
    this.$emit('setService', this.service)
    this.$emit('setPreSaveValidateProps', this.preSaveValidateProps)
    // this.bizId !== '0' 子流程第一步时，路由为了站位，bizId赋值为 '0',此时不获取数据
    if (this.bizId && this.bizId !== '0') {
      let params = {
        entityName: this.entityName,
        bizId: this.bizId,
        detailEntityNameList:this.detailEntityNameList || []
      }
      getFormData(this.service.initial, params).then(async (res) => {
        const result = this.beforeInit ? await this.beforeInit(res) : ''
        if (result) {
          this.formData = result
        } else {
          this.formData = res.data.entityObject
          this.detailParamList = res.data.detailParamList
        }
      })
    }
  },
  beforeDestroy() {
    this.formData = {}
    this.detailParamList = []
  }
}
